package com.xin.uploadanddownload.utils;



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * FileInputStream和FileReader读取中文文件有什么区别
 * utf-8中中文占用几个字节 占用3个字节
 */
public class FileReadAndWrire {
   public static String path = "D:\\新建文本文档.txt";
   public static String path2 = "D:\\写入文本文档.txt";
    public static void main1(String[] args)  {

        try {
            FileInputStream fs = new FileInputStream(path);
            /**
             * 这里读的是ascii码
             * 中文会乱码
             */
            int read = fs.read();
            System.out.println(read);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main2(String[] args) throws FileNotFoundException {
        FileInputStream fs = new FileInputStream(path);
        byte[] bytes = new byte[30];
        int read = 0;
        try {
            read = fs.read(bytes);

            System.out.println(read);

            System.out.println(Arrays.toString(bytes));
            // 这里要与而文件编码保持一致
            String s = new String(bytes, "UTF-8");
            System.out.println(s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String content="这里是写入文件的内容";
        try {
            FileOutputStream fos = new FileOutputStream(path2);
            fos.write(content.getBytes("UTF-8"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
